const list = require('./noNeedTokenList')
const jwt = require('./token')
const sql = require('../sql/sql')
/* 失败返回的提示数据 */
const reqError = {
    code:500,
    msg:'你没有请求权限'
}

/* 正文 */
module.exports = (req,res,next)=>{
        const {baseUrl} = req
    
        /* 判断是否在列表内 */
        const respose = list.some((value)=>{
            return value == baseUrl
        })
    
        
        if(!respose){
            next()
        }else{
            const token = req.headers.authorization||''
            if(token){
              const noSign =jwt.decode(token)
              /* 判断解码是否成功 */
              
               if(!noSign){
                 res.send(reqError)
                 return
               }

              const params = []
              for(item in noSign){
                params.push(noSign[item])
              }
              

              /* 查询数据 */
              sql('login',params).then(result=>{
                  if(result.length > 0){
                      next()
                  }else{
                    res.send(reqError)
                  }
              })
            }else{
                res.send(reqError)
                return
            }
           
           
        }
    }
